package com.xlzcx.xltz;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;

import cn.hutool.json.JSONUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者: 李咨辰
 * 文件名: Abc.java
 * 路径: com.xlzcx.xltz
 * 时间: 2024-11-19 13:36
 * 版本: 1.0
 * 描述: TODO
 **/

public class Abc {


    public static void main(String[] args) throws IOException {
        Abc abc = new Abc();
        abc.getMap("100000");

    }

    public void getMap(String adcode) throws IOException {
        String url = "https://restapi.amap.com/v3/config/district";
        String argument = "?keywords=" + adcode + "&subdistrict=5&key=29fba64b6474d5732699b53c09a3129e";
        URL obj = new URL(url + argument);//创建连接
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");//get请求
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        JSONObject jsonObject = new JSONObject(response.toString());
        JSONArray js2 = new JSONArray(jsonObject.getJSONArray("districts"));


        List<Map> mapList = JSONUtil.toList(js2, Map.class);

        List<String> n = new ArrayList<>();
        Abc abc = new Abc();
        for (Map map1 : mapList) {
            AMap aMap1 = new AMap(
                    map1.getAdcode() + "00",//编号
                    "0",//父级编号
                    "[" + map1.getAdcode() + "00" + "]",//祖级编号
                    "中华人民共和国",//名称
                    map1.getCenter(),//坐标
                    null,//城市代码
                    "10000000",//国家编号
                    "中华人民共和国",//国家名称
                    null,//省级编号
                    null,//省级名称
                    null,//市级编号
                    null,//市级名称
                    null,//县级编号
                    null,//县级名称
                    null,//镇
                    null,//镇
                    map1.getLevel());
            abc.add(aMap1);
            for (Map map2 : map1.getDistricts()) {
                AMap aMap2 = new AMap(
                        map2.getAdcode() + "000",
                        map1.getAdcode() + "000",
                        "[" + map1.getAdcode() + "000" + "," + map2.getAdcode() + "000" + "]",
                        map2.getName(),
                        "[" + map2.getCenter() + "]",
                        null,
                        "100000000",
                        "中华人民共和国",
                        map2.getAdcode() + "000",
                        map2.getName(),
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        map2.getLevel());
                abc.add(aMap2);
                for (Map map3 : map2.getDistricts()) {
                    AMap aMap3 = new AMap(
                            map3.getAdcode() + "000",
                            map2.getAdcode() + "000",
                            "[" + map1.getAdcode() + "000" + "," + map2.getAdcode() + "000" + "," + map3.getAdcode() + "000" + "]",
                            map3.getName(),
                            "[" + map3.getCenter() + "]",
                            map3.getCitycode() + "000",
                            "100000000",
                            "中华人民共和国",
                            map2.getAdcode() + "000",
                            map2.getName(),
                            map3.getAdcode() + "000",
                            map3.getName(),
                            null,
                            null,
                            null,
                            null,
                            map3.getLevel());
                    abc.add(aMap3);
                    for (Map map4 : map3.getDistricts()) {
                        AMap aMap4 = new AMap(
                                map4.getAdcode() + "000",
                                map3.getAdcode() + "000",
                                "[" + map1.getAdcode() + "000" + "," + map2.getAdcode() + "000" + "," + map3.getAdcode() + "000" + "," + map4.getAdcode() + "000" + "]",
                                map4.getName(),
                                "[" + map4.getCenter()+"]",
                                map4.getCitycode() + "000",
                                "100000000",
                                "中华人民共和国",
                                map2.getAdcode() + "000",
                                map2.getName(),
                                map3.getAdcode() + "000",
                                map3.getName(),
                                map4.getAdcode() + "000",
                                map4.getName(),
                                null,
                                null,
                                map4.getLevel());

                        if (map4.getLevel().equals("district")) {
                            abc.add(aMap4);
                            int num = 1;
                            for (Map map5 : map4.getDistricts()) {
                                String a = String.valueOf(num);
                                if (num < 10) {
                                    a = "00" + a;
                                } else if (num < 100) {
                                    a = "0" + a;
                                }
                                AMap aMap5 = new AMap(
                                        map5.getAdcode() + a,
                                        map4.getAdcode() + "000",
                                        "[" + map1.getAdcode() + "000" + "," + map2.getAdcode() + "000" + "," + map3.getAdcode() + "000" + "," + map4.getAdcode() + "000," + map5.getAdcode() + a + "]",
                                        map5.getName(),
                                        "[" + map5.getCenter() + "]",
                                        map5.getCitycode() + "000",
                                        "100000000",
                                        "中华人民共和国",
                                        map2.getAdcode() + "000",
                                        map2.getName(),
                                        map3.getAdcode() + "000",
                                        map3.getName(),
                                        map4.getAdcode() + "000",
                                        map4.getName(),
                                        map5.getAdcode() + a,
                                        map5.getName(),
                                        map5.getLevel());
                                abc.add(aMap5);
                                num++;
                            }


                        } else {
                            String s = map4.getAdcode() + "000" + "-" + map1.getName() + map2.getName() + map3.getName() + map3.getCenter() + "-" + map3.getCitycode();
                            if (!n.contains(s)) {
                                n.add(s);
                            }
                        }
//                        for (Map map5 : map4.getDistricts()) {
//                            abc.add(map5);
//                        }
                    }
                }
            }
        }
        for (String name : n) {
            System.out.println(name);
        }
    }


    private int aaaaa = 0;

    public void add(AMap aMap) {
        // 数据库连接信息
        String url = "jdbc:mysql://127.0.0.1:3306/item";  // 数据库URL
        String username = "root";  // 数据库用户名
        String password = "LZC#mysql#0620";  // 数据库密码

        Connection connection = null;
        PreparedStatement statement = null;

        try {
            // 1. 加载 MySQL JDBC 驱动（对于 MySQL 8.0 或更高版本，可以省略此步骤，JDBC 自动加载）
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2. 获取数据库连接
            connection = DriverManager.getConnection(url, username, password);

            // 3. 编写插入数据的 SQL 语句
            String sql =
                    "insert into d_region " +
                            "(id,parent_id,ancestors,name,center,country_id,country_name," +
                            "province_id,province_name,city_id,city_name,district_id," +
                            "district_name,street_id,street_name,level) values (" +
                            "?," +
                            "?," +
                            "?," +
                            "?," +
                            "?," +
                            "?," +
                            "?," +
                            "?," +
                            "?," +
                            "?," +
                            "?," +
                            "?," +
                            "?," +
                            "?," +
                            "?," +
                            "?" +
                            ")";

            // 4. 创建 PreparedStatement 对象
            statement = connection.prepareStatement(sql);

            // 5. 设置参数
            statement.setString(1, aMap.getId());
            statement.setString(2, aMap.getParentId());
            statement.setString(3, aMap.getAncestors());
            statement.setString(4, aMap.getName());
            statement.setString(5, aMap.getCenter());
            statement.setString(6, aMap.getCountryId());
            statement.setString(7, aMap.getCountryName());
            statement.setString(8, aMap.getProvinceId());
            statement.setString(9, aMap.getProvinceName());
            statement.setString(10, aMap.getCityId());
            statement.setString(11, aMap.getCityName());
            statement.setString(12, aMap.getDistrictId());
            statement.setString(13, aMap.getDistrictName());
            statement.setString(14, aMap.getStreetId());
            statement.setString(15, aMap.getStreetName());
            statement.setString(16, aMap.getLevel());

            // 6. 执行 SQL 插入操作
            int rowsAffected = statement.executeUpdate();
            aaaaa++;
            System.out.println(aMap.getName() + "-添加成功" + aaaaa);  // 输出影响的行数

//            // 插入第二条数据
//            statement.setInt(1, 2);
//            statement.setString(2, "Another Test");
//            rowsAffected = statement.executeUpdate();
//            System.out.println("Rows affected: " + rowsAffected);

        } catch (ClassNotFoundException e) {
//            System.out.println("MySQL JDBC Driver not found.");
            e.printStackTrace();
        } catch (SQLException e) {
//            System.out.println("Connection or SQL error.");
            e.printStackTrace();
        } finally {
            // 7. 关闭连接和 statement
            try {
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
